//DC
#pragma semicolon 1 // Force strict semicolon mode.

#include <sourcemod>
#include <sdktools>
#include <tf2_stocks>
#include <tloot>
#include <sdkhooks>

#define PLUGIN_NAME              "psyFx - BottleOfRum'"
#define PLUGIN_AUTHOR            "Thrawn"
#define PLUGIN_VERSION           "1.0.0.0"
#define PLUGIN_CONTACT           "http://aaa.einfachonline.net/"

#define SOUND_BOTTLEOFRUM		"vo/demoman_gibberish02.wav"

new g_iEffectNum[8] = {-1,...};
new Handle:g_clientEffectTimer[MAXPLAYERS + 1][2];

new bool:g_bHasBottleOfRum[MAXPLAYERS + 1] = {false, ...};
new bool:g_bHasBleifinger[MAXPLAYERS + 1] = {false, ...};

public Plugin:myinfo = {
    name        = PLUGIN_NAME,
    author      = PLUGIN_AUTHOR,
    description = PLUGIN_NAME,
    version     = PLUGIN_VERSION,
    url         = PLUGIN_CONTACT
};

public OnPluginStart() {
	HookEvent("player_death", Event_PlayerDeath);
}

public OnMapStart()
{
	//PrecacheModel(MODEL_CONE, true);
	PrecacheSound(SOUND_BOTTLEOFRUM, true);
}

public OnAllPluginsLoaded() {
	if(LibraryExists("tloot")) {
		g_iEffectNum[0] = Loot_RegEffect("Bottle of Rum","You'll get used to it...");
	}		
}

public OnEffectActivated(client,effectId) {
	if(effectId == g_iEffectNum[0]) {
		EffectBottleOfRum(client);
	}		
}

public DisableEffect(client,effectId) {
	for(new i = 0; i < sizeof(g_iEffectNum); i++) {
		if(effectId == g_iEffectNum[i]) {
			if(g_clientEffectTimer[client][0] != INVALID_HANDLE)
				TriggerTimer(g_clientEffectTimer[client][0]);
			if(g_clientEffectTimer[client][1] != INVALID_HANDLE)
				TriggerTimer(g_clientEffectTimer[client][1]);
		}
	}
}

public Action:Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
{
	new client = GetClientOfUserId(GetEventInt(event, "userid"));
	
	if(g_bHasBottleOfRum[client]) {
		SDKUnhook(client, SDKHook_PreThink, OnPreThink);
		g_bHasBottleOfRum[client] = false;
	}
	
	return Plugin_Continue;
}

public OnPreThink(client)
{	
	new iButtons = GetClientButtons(client);
	
	if(g_bHasBleifinger[client]) {
		iButtons &= IN_ATTACK;
		SetEntProp(client, Prop_Data, "m_nButtons", iButtons);
	}
	
	if(g_bHasBottleOfRum[client] && (GetEntityFlags(client) & (FL_ONGROUND))) {
		if(iButtons & IN_FORWARD)
			ApplyForce(client, 0, 300.0);

		if(iButtons & IN_BACK)
			ApplyForce(client, 2, 300.0);

		if(iButtons & IN_MOVELEFT)
			ApplyForce(client, 3, 300.0);

		if(iButtons & IN_MOVERIGHT)
			ApplyForce(client, 1, 300.0);
	}
}

public ApplyForce(client, direction, Float:amount) {
	new Float:ang[3], Float:vec[3];
	GetClientEyeAngles(client, ang);
	ang[0] = 0.0;
	
	if(direction == 0) {
		//North --> South
		new Float:angBack[3];			
		angBack = ang;
		angBack[1] += 180.0;				
		
		GetAngleVectors(angBack, vec, NULL_VECTOR, NULL_VECTOR); 		
	}
	
	if(direction == 1) {
		//East --> West		
		new Float:angBack[3];			
		angBack = ang;
		angBack[1] += 90.0;				
		
		GetAngleVectors(angBack, vec, NULL_VECTOR, NULL_VECTOR); 		
	}
	
	if(direction == 2) {
		//South --> North		
		new Float:angBack[3];			
		angBack = ang;
		//angBack[1] -= 180.0;				
		
		GetAngleVectors(angBack, vec, NULL_VECTOR, NULL_VECTOR); 		
	}
	
	if(direction == 3) {
		//West --> East
		new Float:angBack[3];			
		angBack = ang;
		angBack[1] -= 90.0;				
		
		GetAngleVectors(angBack, vec, NULL_VECTOR, NULL_VECTOR); 		
	}	

	ScaleVector(vec, amount);
	
	new Float:playerspeed[3];
	GetEntPropVector(client, Prop_Data, "m_vecVelocity", playerspeed);
	AddVectors(vec, playerspeed, vec);

	
	TeleportEntity(client, NULL_VECTOR, NULL_VECTOR, vec);
	
}

public EffectBottleOfRum(client) {
	g_bHasBottleOfRum[client] = true;
	
	g_clientEffectTimer[client][0] = CreateTimer(12.0, Timer_BottleOfRumEnd, client);
	SDKHook(client, SDKHook_PreThink, OnPreThink);
	
	LootFx_EmitSoundClient(SOUND_BOTTLEOFRUM, client);
}

public Action:Timer_BottleOfRumEnd(Handle:timer, any:client) {
	g_bHasBottleOfRum[client] = false;
	g_clientEffectTimer[client][0] = INVALID_HANDLE;

	SDKUnhook(client, SDKHook_PreThink, OnPreThink);
	
	if (client && IsClientInGame(client) && !IsClientInKickQueue(client)) {
		Loot_EffectIsNowDisabled(client);
	}	
}